package com.heatup.apt.common.util.Netty;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * 1. LogEventEncoder 创建了 DatagramPacket 消息类发送到指定的 InetSocketAddress
 * 2. 写文件名到 ByteBuf
 * 3. 添加一个 SEPARATOR
 * 4. 写一个日志消息到 ByteBuf
 * 5. 添加新的 DatagramPacket 到出站消息
 * Created by blank on 2017/11/20 下午6:20
 */
public class LogEventEncoder extends MessageToMessageEncoder<LogEvent> {

    private final InetSocketAddress remoteAddress;

    public LogEventEncoder(InetSocketAddress remoteAddress) { //1
        this.remoteAddress = remoteAddress;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, LogEvent logEvent, List<Object> out) throws Exception {

        byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8); //2
        byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8);
        ByteBuf buf = channelHandlerContext.alloc().buffer(file.length + msg.length + 1);
        buf.writeBytes(file);
        buf.writeByte(LogEvent.SEPARATOR); //3
        buf.writeBytes(msg); //4
//        out.add(new DatagramPacket(buf, remoteAddress)); //5 教程代码报错= =!
    }
}
